package datastructures;

/**
 * @author MI
 * @version 1.0
 * @date 2020/12/19 11:54
 */
public class ArrayQueueDemo {
    /**
     * 数据模拟队列
     */

    public static void main(String[] args) {
        final ArrayQueue arrayQueue = new ArrayQueue(3);
        arrayQueue.addQueue(1);
        arrayQueue.addQueue(2);
        arrayQueue.addQueue(3);
        arrayQueue.showQueue();
        System.out.println(arrayQueue.headQueue());
        arrayQueue.addQueue(5);

        System.out.println(arrayQueue.push());
        System.out.println(arrayQueue.push());
        System.out.println(arrayQueue.push());
        arrayQueue.showQueue();
    }
}

class ArrayQueue {
    private int maxSize;
    private int front;//队列头
    private int rear;//队列尾
    private int[] arr;//存放数据

    public ArrayQueue(int maxSize) {
        this.maxSize = maxSize;
        this.front = -1;//指向队列头部前一个位置
        this.rear = -1;//指向队列尾。(即最后一个数据)
        this.arr = new int[maxSize];
    }

    public boolean isFull() {
        return rear == maxSize - 1;
    }

    /**
     * 判断空
     *
     * @return
     */
    public boolean isEmpty() {
        return rear == front;
    }

    public void addQueue(int n) {
        //判断队列满
        if (isFull()) {
            System.out.println("队列满，无法加入");
            return;
        }
        rear++;
        arr[rear] = n;
    }

    public int push() {
        if (isEmpty()) {
            System.out.println("队列为空");
            throw new RuntimeException("队列空");
        }
        front++;
        return arr[front];
    }

    public void showQueue() {
        if (isEmpty()) {
            System.out.println("show  队列空");
        }
        for (int i : arr) {
            System.out.printf("%d ", i);
        }
        System.out.println();
    }

    /**
     * 显示队列头部
     *
     * @return
     */
    public int headQueue() {
        if (isEmpty()) {
            System.out.println("队列空 没有数据");
            throw new RuntimeException("队列空");
        }
        return arr[front + 1];
    }
}
